package com.groupbuying.customer.service.impl;

import com.groupbuying.customer.constants.CommonConstants;
import com.groupbuying.customer.dao.CustomerDao;
import com.groupbuying.customer.domain.CustomerDO;
import com.groupbuying.customer.pojo.CustomerEnum;
import com.groupbuying.customer.service.*;
import com.groupbuying.customer.utils.ChineseCharacters2PinyinUtils;
import com.groupbuying.customer.utils.DateFormatHelper;
import com.groupbuying.customer.utils.SnowflakeIdWorker;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @anthor wenguoqiang
 * @date: 2019/01/08 18:53
 * @company 蒙泰互联网公司
 * @des
 **/
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Override
    public CustomerDO get(String uuid) {
        return customerDao.get(uuid);
    }

    @Override
    public List<CustomerDO> list(Map<String, Object> map) {
        return customerDao.list(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return customerDao.count(map);
    }

    @Override
    public int save(CustomerDO customer) {
        return customerDao.save(customer);
    }

    @Override
    public int update(CustomerDO customer) {
        return customerDao.update(customer);
    }

    @Override
    public int remove(String uuid) {
        return customerDao.remove(uuid);
    }

    @Override
    public int batchRemove(String[] uuids) {
        return customerDao.batchRemove(uuids);
    }


    @Override
    public CustomerDO getByUuid(String customerUuid) {
        return customerDao.get(customerUuid);
    }

    @Override
    public boolean checkCustomerNameExist(String customerName) {
        Map map = new HashMap();
        map.put("customername", customerName);
        map.put("delflag", "1");
        List list = customerDao.list(map);
        if (list != null) {
            return list.size()>0;
        }
        return false;
    }

    @Override
    public boolean checkMobileExist(String mobile) {
        Map map = new HashMap();
        map.put("mobile", mobile);
        map.put("delflag", "1");
        List list = customerDao.list(map);
        if (list != null) {
            return list.size()>0;
        }
        return false;
    }

    @Override
    public CustomerDO getCustomerModelByLoginNameOrMobileOrEmail(String loginNameOrMobileOrEmail) {
        Map map = new HashMap();
        List<CustomerDO> list = null;
        map.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
        map.put("mobile", loginNameOrMobileOrEmail);
        list = customerDao.list(map);
        if(list != null && list.size() > 0){
            return list.get(0);
        }

        map.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
        map.put("email", loginNameOrMobileOrEmail);
        list = customerDao.list(map);
        if(list != null && list.size() > 0){
            return list.get(0);
        }

        map.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
        map.put("customername", loginNameOrMobileOrEmail);
        list = customerDao.list(map);
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<CustomerDO> getCustomerByMobile(String mobile) {
        return customerDao.getCustomerByMobile(mobile);
    }


}
